package platform.database;

import com.alibaba.fastjson.JSONObject;
import framework.sepal4java.common.struct.ConstantEnum.OrderType;

public class Order {
    private String column;
    private OrderType order;
    private JSONObject jo = null;
    private String json = null;

    public Order(String column, OrderType order) {
        this.column = column;
        this.order = order;
    }

    public Order(String json) {
        try {
            jo = JSONObject.parseObject(json);
            this.json = json;
            column = jo.getString("column");
            order = OrderType.fromString(jo.getString("order"));
        } catch (Exception e) {
            String[] strs = json.split(" ");
            column = strs[0];
            order = OrderType.fromString(strs[1]);
        }
    }

    public Order(JSONObject jo) {
        this.jo = jo;
        column = jo.getString("column");
        order = OrderType.fromString(jo.getString("order"));
    }

    public String toJSONString() {
        if (json == null) {
            jo = new JSONObject(2);
            jo.put("column", column);
            jo.put("order", order);
            json = jo.toJSONString();
        }
        return json;
    }

    public JSONObject toJSONObject() {
        if (jo == null) {
            jo = new JSONObject(2);
            jo.put("column", column);
            jo.put("order", order);
        }
        return jo;
    }

    @Override
    public String toString() {
        return toJSONString();
    }

    public String getColumn() {
        return column;
    }

    public void setColumn(String column) {
        this.column = column;
    }

    public OrderType getOrder() {
        return order;
    }

    public void setOrder(OrderType order) {
        this.order = order;
    }
}
